package kr.hkit.androidcomplete02.fragment;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class SaveFragmentState extends Activity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.savefragmentstate);
		}
		
		public static class CounterFragment extends Fragment{
			@Override
			public View onCreateView(LayoutInflater inflater,
					ViewGroup container, Bundle savedInstanceState) {
			super.onCreateView(inflater, container, savedInstanceState);
			
			View root = inflater.inflate(R.layout.counterfrg, container, false);
			
			Button btnIncrease = (Button) root.findViewById(R.id.btnincrease);
			final TextView textCounter = (TextView) root.findViewById(R.id.txtCounter);
			
			if (savedInstanceState != null) { //저장되어있으면
				textCounter.setText(Integer.toString(savedInstanceState.getInt("counter")));
			}
			
			btnIncrease.setOnClickListener(new Button.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int count = Integer.parseInt(textCounter.getText().toString());
					textCounter.setText(Integer.toString(count+1));
				}
			});
			
			return root;
			}
		
			@Override
			public void onSaveInstanceState(Bundle outState) {
				super.onSaveInstanceState(outState);
				
				TextView textCounter = (TextView) getView().findViewById(R.id.txtCounter);
				int a = Integer.parseInt(textCounter.getText().toString());
				outState.putInt("counter", a);
			}
		
		}
		
}
